package com.AdvancedDemos.单元测试_反射_注解.反射;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class 反射成员变量练习 {
    public static void main(String[] args) throws FileNotFoundException, IllegalAccessException {
        /**
         * 使用反射做一个简易版的框架
         * 需求:对于任意一个对象，该框架都可以把对象的字段名和对应的值，保存到文件中去
         *
         * 实现步骤:
         * ①定义一个方法，可以接收任意对象。
         * ②每收到一个对象后，使用反射获取该对象的Class对象，然后获取全部的成员变量。
         * ③遍历成员变量，然后提取成员变量在该对象中的具体值。
         * ④把成员变量名、和其值，写出到文件中去即可。
         *
         */

        Student s1 = new Student("黑马吴彦祖", 45, '男', 185.3, "唱跳rap");
        Teacher t1 = new Teacher("播妞", 999.9);
        //需求:把任意对象的字段和其对应的值等信息保存到文件中去
        ObjectFrame.saveObject(s1);//把学生对象保存到文件
        ObjectFrame.saveObject(t1);//把老师对象保存到文件
//        ObjectFrame.saveObject(new String("My name is TianpeiWang"));

        /**
         * ---Student---
         * name = 黑马吴彦祖
         * age = 45
         * sex = 男
         * height = 185.3
         * hobby = 唱跳rap
         * ---Teacher---
         * name = 播妞
         * salary = 999.9
         */

    }
}

class ObjectFrame {
    //对象框架
    //目标:保存任意对象的字段和其数据到文件中去
    public static void saveObject(Object obj) throws IllegalAccessException, FileNotFoundException {
        PrintStream ps = new PrintStream(new FileOutputStream("helloworld\\src\\com\\AdvancedDemos\\单元测试_反射_注解_代理\\反射\\objects.txt", true));//打开打印流管道
        //obj是任意对象,到底有多少个字段要保存?
        Class clazz = obj.getClass();//获取obj的字节码文件对象的类型

        String simpleName = clazz.getSimpleName();//获取obj的类名
        ps.println("---" + simpleName + "---");

        Field[] fields = clazz.getDeclaredFields();//获取全部成员变量
        //遍历每个成员变量
        for (Field field : fields) {
            String name = field.getName();//拿到每个成员变量的名字
            field.setAccessible(true);//禁止检查obj的属性的访问权限,允许类外访问
            String value = field.get(obj) + "";//拿到成员变量在对象中的值,转换成字符串
            ps.println(name + " = " + value);
        }
        ps.close();//关闭打印流管道
    }
}

class Student {
    private String name;
    private int age;
    private char sex;
    private double height;
    private String hobby;

    public Student() {
    }

    public Student(String name, int age, char sex, double height, String hobby) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.height = height;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

class Teacher {
    private String name;
    private double salary;

    public Teacher() {

    }

    public Teacher(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}
